package io.lvdaxian.mybatis19.test;

import com.alibaba.fastjson2.JSON;
import io.lvdaxian.mybatis19.io.Resources;
import io.lvdaxian.mybatis19.session.SqlSession;
import io.lvdaxian.mybatis19.session.SqlSessionFactory;
import io.lvdaxian.mybatis19.session.SqlSessionFactoryBuilder;
import io.lvdaxian.mybatis19.test.dao.IActivityDao;
import io.lvdaxian.mybatis19.test.po.Activity;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;

public class ApiTest01 {
  
  private SqlSession sqlSession;
  
  @Before
  public void beforeHandler() throws IOException {
    // 1. 从SqlSessionFactory中获取SqlSession
    Reader reader = Resources.getResourceAsReader("mybatis-config-datasource.xml");
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    
    sqlSession = sqlSessionFactory.openSession();
  }
  
  @Test
  public void test_queryActivityById() throws IOException {
    // 2. 请求对象
    Activity req = new Activity();
    req.setActivityId(100001L);
    
    // 3.2 获取映射器对象
    IActivityDao dao01 = sqlSession.getMapper(IActivityDao.class);
    System.out.println("测试结果01: " + JSON.toJSONString(dao01.queryActivityById(req)));
    
    // 4.1 开启 Session
    // 4.2 获取映射器对象
    IActivityDao dao02 = sqlSession.getMapper(IActivityDao.class);
    System.out.println("测试结果02: " + JSON.toJSONString(dao02.queryActivityById(req)));
  }
}
